package com.hongruan.action.Account;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hongruan.action.BaseAction;
import com.hongruan.bean.Account;
import com.hongruan.bean.Swjg;
import com.hongruan.common.Constants;
import com.hongruan.model.ResultStatusModel;
import com.hongruan.service.AccountService;
import com.hongruan.service.SwjgService;
import com.hongruan.utils.Encrypter;

/**
 * 账户相关操作
 * 
 * @author ZhangWenLong
 * 
 */
@Controller("AccountAction")
@Scope("prototype")
public class AccountAction extends BaseAction {

	private static final long serialVersionUID = 6048710889280509574L;

	private static Logger logger = Logger.getLogger(AccountAction.class);

	private Account account;
	
	@Resource
	private AccountService accountService;
	@Resource
	private SwjgService swjgService;

	public String login() {
		logger.info("login() begin");

		String userName = getRequest().getParameter("userName");
		String password = getRequest().getParameter("password");
		
        JSONObject json = new JSONObject();
        ResultStatusModel result = new ResultStatusModel();
        
        try{
        	Account acc = new Account();
        	acc.setUserName(userName);
        	acc.setPassword(Encrypter.md5s(password));
        	
            Account account = this.accountService.read(acc);
            
            if(account != null){
            	getRequest().getSession().setAttribute(Constants.SESSION_USER_KEY, account);
            	
                json.put("exist", "yes");
                json.put("redirectUrl", "welcome.jsp");
                json.put("czryname", account.getYhxm());
                json.put("czryuuid", account.getId());
                if(null != account.getSwjgId()){
                	json.put("swjguuid", account.getSwjgId());
                	Swjg swjg = this.swjgService.getSwjgById(account.getSwjgId());
                	if(null != swjg){
                		json.put("swjgname", swjg.getMc());
                	}else{
                		json.put("swjgname", "-");
                	}
                }else{
                	json.put("swjguuid", "-");
                	json.put("swjgname", "-");
                }
            }else{
                json.put("exist", "no");
            }
            
            result.setSuccess(true);
            
        }catch(Exception e){
            logger.error("login()... error",e);
            
            result.setSuccess(false);
        }
        
        json.put("result", result);
        
        this.writeAjaxResult(json.toString());
        
        return null;
	}
	
	public String logout() {
		logger.info("logout() begin");
		
		getRequest().getSession().invalidate();
        
        return "logout";
	}
	
	public String accountList() {
		logger.info("accountList() begin");

        JSONObject json = new JSONObject();
        ResultStatusModel result = new ResultStatusModel();
        
        try{
            List accountList = this.accountService.accountList(account);
            
            json.put("accountList", accountList);
            
            result.setSuccess(true);
            
        }catch(Exception e){
            logger.error("accountList()... error",e);
            
            result.setSuccess(false);
        }
        
        json.put("result", result);
        
        this.writeAjaxResult(json.toString());
        
        return null;
	}
	
	public String addAccount() {
		logger.info("addAccount() begin");

        JSONObject json = new JSONObject();
        ResultStatusModel result = new ResultStatusModel();
        
        try{
        	account.setPassword(Encrypter.md5s("888888"));
        	account.setIsDel("0");
        	
        	this.accountService.create(account);
        	
            result.setSuccess(true);
            
        }catch(Exception e){
            logger.error("addAccount()... error",e);
            
            result.setSuccess(false);
        }
        
        json.put("result", result);
        
        this.writeAjaxResult(json.toString());
        
        return null;
	}
	
	public String deleteAccount() {
		logger.info("deleteAccount() begin");

        JSONObject json = new JSONObject();
        ResultStatusModel result = new ResultStatusModel();
        
        try{
        	Account acc = this.accountService.read(account.getId());
        	acc.setIsDel("1");
        	
        	this.accountService.update(acc);
            
            result.setSuccess(true);
            
        }catch(Exception e){
            logger.error("deleteAccount()... error",e);
            
            result.setSuccess(false);
        }
        
        json.put("result", result);
        
        this.writeAjaxResult(json.toString());
        
        return null;
	}

	public String modifyAccountPassword(){
		logger.info("modifyAccountPassword() begin");

        JSONObject json = new JSONObject();
        ResultStatusModel result = new ResultStatusModel();
        
        try{
        	Account acc = (Account)getRequest().getSession().getAttribute(Constants.SESSION_USER_KEY);
        	acc = this.accountService.read(acc.getId());
        	acc.setPassword(Encrypter.md5s(account.getPassword()));
        	
        	this.accountService.update(acc);
            
            result.setSuccess(true);
            
        }catch(Exception e){
            logger.error("modifyAccountPassword()... error",e);
            
            result.setSuccess(false);
        }
        
        json.put("result", result);
        
        this.writeAjaxResult(json.toString());
        
		return null;
	}
	
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}
	
}
